<?php

namespace App\Http\Controllers\Admin;

use App\Repository\Sms\SmsAgentRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;

// 分销商
class MessageAgentController extends BaseController
{
  public function __construct(SmsAgentRepository $model)
  {
    $this->repoist = $model;
  }
  public function store(Request $request)
  {
    return $this->repoist->add($request);
  }
  public function index(Request $request)
  {
    $where = [];
    if (!empty($request->get('number'))) {
      $where['like'] = ['name' => $request->get('number')];
    }
    if (!empty($request->get('pageNum'))) {
      $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
      $where['limit']['number'] = $request->get('pageSize');
    }

    if ($request->user->role_id != 1) {
      $where['where']['shop_id'] = $request->user->shop_id;
    }
    $where['select'] = ['name', 'shop_id', 'id', 'created_at', 'updated_at'];
    $data =  $this->repoist->index($where);
    return $data;
  }
  public function save(Request $request)
  {
    if (!$request->get('id')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = ['id' => $request->get('id'), 'shop_id' => $request->user->shop_id];
    return $this->repoist->edit($where, $request);
  }
  public function  show(Request $request, $id)
  {
    $where = ['id' => $id, 'shop_id' => $request->user->shop_id];
    //查询一条数据
    $return = $this->repoist->getOne($where, ['id', 'name', 'content']);

    return $this->returnMsg(200, '查询成功', $return);
  }

  public function listss(Request $request)
  {
    $where = [];
    if (!empty($request->get('name'))) {
      $where['where'] = ['name' => $request->get('name')];
    }
    $where['where']['shop_id'] = $request->user->shop_id;
    // var_dump($where);
    $return = $this->repoist->lists($where, 1, 99999);
    return $return;
  }

  public function destroy(Request $request, $id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    return $this->repoist->del($where);
  }
  public function updateStatus(Request $request, $id)
  {
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    $update = [
      'status' => $request->get('status'),
    ];
    return $this->repoist->edit($where, $request);
  }
}
